/* 
 * Copyright (c) 2022 Huawei Technologies Co.,Ltd.
 *
 * openGauss is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *           http://license.coscl.org.cn/MulanPSL2
 *        
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

package org.opengauss.mppdbide.view.handler.table;

import org.eclipse.e4.core.di.annotations.CanExecute;
import org.eclipse.e4.core.di.annotations.Execute;

import org.opengauss.mppdbide.bl.serverdatacache.TableMetaData;
import org.opengauss.mppdbide.bl.serverdatacache.TableOrientation;
import org.opengauss.mppdbide.utils.IMessagesConstants;
import org.opengauss.mppdbide.utils.messaging.ProgressBarLabelFormatter;
import org.opengauss.mppdbide.view.handler.IHandlerUtilities;
import org.opengauss.mppdbide.view.handler.ReindexTableWorker;

/**
 * 
 * Title: class
 * 
 * Description: The Class ReindexTableHandler.
 *
 * @since 3.0.0
 */
public class ReindexTableHandler {

    /**
     * Execute.
     */
    @Execute
    public void execute() {
        TableMetaData selTable = IHandlerUtilities.getSelectedTable();
        if (null != selTable) {

            String progressLabel = ProgressBarLabelFormatter.getProgressLabelForTableWithMsg(selTable.getName(),
                    selTable.getNamespace().getName(), selTable.getDatabaseName(), selTable.getServerName(),
                    IMessagesConstants.REINDEX_TABLE_PROGRESS_NAME);
            ReindexTableWorker worker = new ReindexTableWorker(progressLabel, selTable);
            worker.schedule();
        }
    }

    /**
     * Can execute.
     *
     * @return true, if successful
     */
    @CanExecute
    public boolean canExecute() {
        TableMetaData selectTable = IHandlerUtilities.getSelectedTable();
        if (null == selectTable) {
            return false;
        }
        return !IHandlerUtilities.isSelectedTableForignPartition();
    }

}